﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;

namespace Public
{
    public class BarTender_Print
    {
        BarTender.Application btApp;
        BarTender.Format btFormat;
        /// <summary>
        /// 调用Bartender模板打印
        /// </summary>
        /// <param name="lblPath">模板路径</param>
        /// <param name="hTable">Hashtable数据集</param>
        /// <param name="PrintQty">打印张数</param>
        /// <returns></returns>
        public bool Print(string lblPath, System.Collections.Hashtable hTable, int PrintQty)
        {

            //return true;
            //oErrMessage = "";
            if (btApp == null)
            {
                btApp = new BarTender.Application();
            }
            btFormat = btApp.Formats.Open(lblPath, false, "");
            btFormat.PrintSetup.IdenticalCopiesOfLabel = PrintQty;//设置打印时要打印多少份相同的标签的，默认为标签设置
            btFormat.PrintSetup.NumberSerializedLabels = 1;//序列化打印时使用的，当你的标签启动了序列化后，这个属性代表的就是打印的份数，譬如你的序列化初始数据是1，增量为1，NumberSerializedLabels设置为5，那么就会打印出1、2、3、4、5，五个标签出来
            btFormat.PrintSetup.UseDatabase = true;
            string key_i = "";
            //btFormat.NamedSubStrings.
            try
            {
                foreach (string key in hTable.Keys)
                {
                    key_i = key;
                    btFormat.SetNamedSubStringValue(key, (hTable[key] + ""));
                    // btFormat.
                }
            }
            catch (Exception e1)
            {
                throw new Exception("key:" + key_i + "," + e1.Message + ",此错误通常都是模板没有此变量:" + key_i + "造成！");
                return false;
            }
            btFormat.PrintOut(false, false);
            btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
            return true;
        }


        public bool Print2(string lblPath, System.Collections.Hashtable hTable, int PrintQty,string UsePrinter)
        {

            //return true;
            //oErrMessage = "";
            if (btApp == null)
            {
                btApp = new BarTender.Application();
            }
            btFormat = btApp.Formats.Open(lblPath, false, UsePrinter);
            btFormat.PrintSetup.IdenticalCopiesOfLabel = PrintQty;//设置打印时要打印多少份相同的标签的，默认为标签设置
            btFormat.PrintSetup.NumberSerializedLabels = 1;//序列化打印时使用的，当你的标签启动了序列化后，这个属性代表的就是打印的份数，譬如你的序列化初始数据是1，增量为1，NumberSerializedLabels设置为5，那么就会打印出1、2、3、4、5，五个标签出来
            btFormat.PrintSetup.UseDatabase = true;
            string key_i = "";
            //btFormat.NamedSubStrings.
            try
            {
                foreach (string key in hTable.Keys)
                {
                    key_i = key;
                    btFormat.SetNamedSubStringValue(key, (hTable[key] + ""));
                    // btFormat.
                }
            }
            catch (Exception e1)
            {
                throw new Exception("key:" + key_i + "," + e1.Message + ",此错误通常都是模板没有此变量:" + key_i + "造成！");
                return false;
            }
            btFormat.PrintOut(false, false);
            btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
            return true;
        }



        /// <summary>
        /// 调用Bartender模板打印
        /// </summary>
        /// <param name="lblPath">模板路径</param>
        /// <param name="hTable">Hashtable数据集</param>
        /// <param name="PrintQty">打印张数</param>
        /// <returns></returns>
        public System.Drawing.Image PrintPreview(string iTempPath) // System.Drawing.Image string lblPath
        {

            // Declare a BarTender document variable
            // Declare a BarTender messages variable

            BarTender.Messages msgs;

            // Create a new instance of BarTender
            if (btApp == null)
            {
                btApp = new BarTender.Application();
            }
            //btApp = new BarTender.Application();

            // Set the BarTender application visible

            btApp.Visible = false;

            // Open a BarTender document

            btFormat = btApp.Formats.Open(iTempPath, false, "");

            // Export document to a print preview

            BarTender.BtPrintResult res;

            res = btFormat.ExportPrintPreviewToImage("C:\\", "Label_Preview.jpg", "jpg", BarTender.BtColors.btColors24Bit, 200, 13117215, BarTender.BtSaveOptions.btSaveChanges, true, true, out msgs);

            // End the BarTender process

            btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
            Image img = null;
            using (FileStream image = new FileStream("C:\\Label_Preview1.jpg", FileMode.Open))
            {
                return img = Image.FromStream(image);
            }
            //Image img = Image.FromStream("F:\\");
        }

        /// <summary>
        /// 调用Bartender模板打印
        /// </summary>
        /// <param name="lblPath">模板路径</param>
        /// <param name="hTable">Hashtable数据集</param>
        /// <param name="PrintQty">打印张数</param>
        /// <param name="cboPrinters">打印机</param>
        /// <returns></returns>
        public bool Print(string lblPath, System.Collections.Hashtable hTable, int PrintQty, string cboPrinters)
        {
            //oErrMessage = "";
            //return true;
            if (btApp == null)
            {
                btApp = new BarTender.Application();
            }
            btFormat = btApp.Formats.Open(lblPath, false, "");
            btFormat.PrintSetup.IdenticalCopiesOfLabel = PrintQty;//打印张数
            btFormat.PrintSetup.NumberSerializedLabels = 1;//打印序列数
            btFormat.PrintSetup.Printer = cboPrinters.ToString();//指定打印机
            //btFormat.Databases.
            string key_i = "";
            //btFormat.NamedSubStrings.
            try
            {
                foreach (string key in hTable.Keys)
                {
                    key_i = key;
                    btFormat.SetNamedSubStringValue(key, (hTable[key] + ""));
                    // btFormat.
                }
            }
            catch (Exception e1)
            {
                throw new Exception("key:" + key_i + "," + e1.Message + ",此错误通常都是模板没有此变量:" + key_i + "造成！");
                //return false;
            }
            btFormat.PrintOut(false, false);
            btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
            return true;
        }
        public void Quit()
        {
            btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
        }
    }
}
